package com.travel.rabbit.aop;


import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * @author 云澜
 * @date 2023/5/16 10:34 上午
 */
@Slf4j
public class LogAspect {

    protected static final String SESSION_USER_NAME = "token";

    protected String convent(Object args){
        try {
            return JSONUtil.toJsonStr(args);
        }catch (Exception e){
            return "";
        }
    }

    protected long excTime(long startTime){
        long endTime = System.currentTimeMillis();
        return (endTime-startTime)/1000;
    }

    protected Method getMethod(JoinPoint point) {
        MethodSignature methodSignature = (MethodSignature) point.getSignature();
        Class<?> targetClass = point.getTarget().getClass();
        try {
            return targetClass.getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
        } catch (NoSuchMethodException e) {
            return null;
        }
    }

    /**
     * @description 获取用户信息
     */
    protected String getUser(JoinPoint point){
        try {
            log.info("获取请求参数");
            Object[] args = point.getArgs();
            HttpServletRequest request = null;
            if (ArrayUtils.isNotEmpty(args)) {
                for (Object arg : args) {
                    if(arg instanceof HttpServletRequest){
                        request = (HttpServletRequest)arg;
                        break;
                    }
                }
            }
            if(request!=null){
                return (String) request.getAttribute(SESSION_USER_NAME);
            }
        }catch (Exception e){
            log.error("get user is null",e.getMessage());
        }
        return "-";
    }

    protected String getParams(JoinPoint point) {
        log.info("获取请求参数");
        Object[] args = point.getArgs();
        MethodSignature methodSignature = (MethodSignature) point.getSignature();
        String[] parameterNames = methodSignature.getParameterNames();
        StringBuffer sb = new StringBuffer();

        if (ArrayUtils.isNotEmpty(args)) {
            for (int i = 0; i < args.length; i++) {
                sb.append(convent(parameterNames[i]))
                        .append(":")
                        .append(convent(args[i]))
                        .append(";");
            }
        }
        return sb.toString();
    }

    protected String getParams2(ProceedingJoinPoint pj){
        Object[] objects = pj.getArgs();
        StringBuilder args = new StringBuilder();
        for (int i = 0; i < objects.length; i++) {
            String s = JSONUtil.toJsonStr(objects[i]);
            if (i == objects.length - 1){
                args.append(s);
            }else {
                args.append(s).append(",");
            }
        }
        return args.toString();
    }
}
